<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>物业管理系统</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/plugins/bootstrap-table/css/bootstrap-table.css">
</head>
<style>
</style>
<body style="padding-bottom: 90px; background-color: #ECF0F5;">
	<section class="content" style="margin: 15px; background-color: white;">
		<div class="row">
			<div class="col-sm-12">
				<table id="table">
					<thead style="background-color: #00C0EF">
						<tr style="background-color: #00C0EF">
							<!-- 							<th class="text-center" data-field=""></th> -->
							<th class="text-center" data-field="userAccount">用户账号</th>
							<th class="text-center" data-field="userName">用户姓名</th>
							<th class="text-center" data-field="roleName">角色</th>
							<!-- 							<th class="text-center" data-field="communityName">所属社区</th> -->
							<th class="text-center" data-field="userMobile">手机</th>
							<th class="text-center" data-field="id">操作</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
	</section>
	<!-- Large modal -->
	<div id="modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">用户编辑</h4>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<label for="user_account" class="control-label">用户名:</label> <input type="text" class="form-control" id="user_account" onchange="checkAccount(this)">
					</div>
					<div class="form-group">
						<label for="user_name" class="control-label">用户姓名:</label> <input type="text" class="form-control" id="user_name">
					</div>
					<div class="form-group">
						<label for="role_id" class="control-label">选择角色:</label> <select id="role_id" class="form-control">
							<!-- 							<option value="">请选择...</option> -->
							<!-- 							<option value="0084ae3f-3764-48d1-989c-0e9241e178a9">超级管理员</option> -->
						</select>
					</div>
					<div class="form-group">
						<label class="control-label">手机：</label> <input type="text" class="form-control" id="user_mobile">
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary" onclick="submitUser();">提交</button>
				</div>
			</div>
		</div>
	</div>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-zh-CN.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-export.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/tableExport.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-toolbar.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-filter-control.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script>
	var oldUserAccount;
	var userId = "";
	var isChecked = true;
	function actionFormatter(value, row, index) {
		return [ "<button class='btn btn-primary btn-xs glyphicon glyphicon-pencil' onclick='showModal(\"" + row.userId + "\");'>编辑</button>",
				"&nbsp;<button class='btn btn-danger btn-xs glyphicon glyphicon-trash' onclick='delUser(\"" + row.userId + "\")'>删除</button>",
				"&nbsp;<button class='btn btn-danger btn-xs glyphicon glyphicon-repeat' onclick='resetPass(\"" + row.userId + "\")'>重置密码</button>", ].join('');
	}
	var table_dataset = [ {
		field : 'userAccount',
		align : 'center',
	}, {
		field : 'userName',
		align : 'center',
	}, {
		field : 'roleName',
		align : 'center',
	}, {
		field : 'userMobile',
		align : 'center',
	}, {
		align : 'center',
		width : 200,
		formatter : actionFormatter,
	} ];
	$('#table').bootstrapTable({
		url : "${pageContext.request.contextPath}/selectUsers",
		idField : "id",
		columns : table_dataset,
		striped : true, //是否显示行间隔色
		pagination : true, //是否显示分页
		pageSize : 10, //每页的记录行数
		pageList : [ 10, 25, 50, 100 ], //可供选择的每页的行数
		search : false, //是否显示搜索
		showRefresh : true,
		sidePagination : "server", //表示服务端请求  
		queryParamsType : "limit",
		queryParams : function queryParams(params) { //设置查询参数  
			var param = {
				offset : params.offset,
				limit : params.limit
			};
			return param;
		},
		toolbar : "<button class='btn btn-primary glyphicon glyphicon-plus' onclick='showModal(\"" + '' + "\");'>新增数据</button>",
		// 						+"&nbsp;<button class='btn btn-danger glyphicon glyphicon-trash'>批量删除</button>",
		icons : {
			paginationSwitchDown : 'glyphicon-collapse-down icon-chevron-down',
			paginationSwitchUp : 'glyphicon-collapse-up icon-chevron-up',
			refresh : 'glyphicon-refresh icon-refresh',
			toggle : 'glyphicon-list-alt icon-list-alt',
			columns : 'glyphicon-th icon-th',
			detailOpen : 'glyphicon-plus icon-plus',
			detailClose : 'glyphicon-minus icon-minus',
			"export" : 'glyphicon-export icon-share'
		},
		showExport : true,
		exportDataType : 'basic',
		exportTypes : [ 'json', 'xml', 'csv', 'txt', 'sql', 'excel' ]
	});

	$(function() {
		$.ajax({
			url : "${pageContext.request.contextPath}/selectAllRoles",
			type : "post",
			dataType : "json",
			success : function(data) {
				var roles = "<option value=''>请选择...</option>";
				for (var i = 0; i < data.length; i++) {
					if (data[i].roleId != "0084ae3f-3764-48d1-989c-0e9241e178a9") {
						roles += "<option value='"+data[i].roleId+"'>" + data[i].roleName + "</option>";
					}
				}
				$("#role_id").html(roles);
			},
			error : function() {
			}
		});
	});

	function showModal(id) {
		if (id == "081e960b-fb61-11e7-97c4-00163e0f8136") {
			layer.msg('超级用户无法修改!', {
				icon : 2,
				time : 800
			});
			return;
		}
		if (id == "") {
			userId = '';
			oldUserAccount = '';
			$("#user_account").val('');
			$("#user_name").val('');
			$("#community_name").val('');
			$("#role_id").val('');
			$("#user_mobile").val('');
			$("#modal").modal("show");
		} else {
			userId = id;
			$.ajax({
				url : "${pageContext.request.contextPath}/selectUserById",
				type : "get",
				data : {
					userId : userId
				},
				dataType : "json",
				success : function(data) {
					oldUserAccount = data.UserAccount;
					$("#user_account").val(data.userAccount);
					$("#user_name").val(data.userName);
					$("#role_id").val(data.roleId);
					$("#community_name").val(data.communityName);
					$("#user_mobile").val(data.userMobile);
					$("#modal").modal("show");
				},
				error : function() {
					layer.msg('系统异常!', {
						icon : 2,
						time : 800
					});
				}
			});
		}
	}

	function checkAccount(obj) {
		$.ajax({
			url : "${pageContext.request.contextPath}/checkUserAccount",
			type : "post",
			async : false,
			data : {
				userAccount : obj.value
			},
			dataType : "json",
			success : function(data) {
				if (data.status == 'y') {
					isChecked = false;
				} else {
					isChecked = true;
				}
			},
			error : function() {
			}
		});
	}

	function submitUser() {
		var userAccount = $('#user_account').val();
		var userName = $('#user_name').val();
		var communityName = $("#community_name").val();
		var roleId = $('#role_id').val();
		var userMobile = $('#user_mobile').val();
		if (userAccount === '' || userName === '' || communityName === '' || roleId === '' || userMobile === '') {
			layer.alert("新增的数据中填写不完整！");
			return false;
		}
		if (isChecked & userId == '') {
			layer.alert("用户名已被使用！");
			return false;
		}
		if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(userMobile))) {
			layer.alert("不是完整的11位手机号或者正确的手机号前七位！");
			return false;
		}
		var url = userId === '' ? '${pageContext.request.contextPath}/insertUser' : '${pageContext.request.contextPath}/updateUser';
		$.ajax({
			url : url,
			type : "post",
			data : {
				userId : userId,
				userAccount : userAccount,
				userName : userName,
				communityName : communityName,
				roleId : roleId,
				userMobile : userMobile
			},
			dataType : "json",
			success : function(data) {
				var result = data['result'];
				if (result === 1) {
					layer.msg('操作成功!', {
						icon : 1,
						time : 800
					});
					setTimeout(function() {
						$("#table").bootstrapTable('refresh');
						$("#modal").modal("hide");
					}, 1000);
				} else {
					layer.msg('操作失败!', {
						icon : 2,
						time : 800
					});
				}
			},
			error : function() {
				layer.msg('系统繁忙，请稍后再试!', {
					icon : 2,
					time : 800
				});
			}
		});

		return false;
	}

	function delUser(id) {
		if (id == "081e960b-fb61-11e7-97c4-00163e0f8136") {
			layer.msg('超级用户无法删除!', {
				icon : 2,
				time : 800
			});
			return;
		}
		layer.confirm("是否要删除用户？", function(index) {
			$.ajax({
				url : "${pageContext.request.contextPath}/deleteUser",
				type : "post",
				data : {
					userId : id
				},
				dataType : "json",
				success : function(data) {
					if (data.result === 1) {
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
						setTimeout(function() {
							$("#table").bootstrapTable('refresh');
						}, 500);
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				},
				error : function() {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 5,
						time : 800
					});
				}
			});
		});
	}

	function resetPass(id) {
		layer.confirm("是否要重置密码为123456？", function(index) {
			$.ajax({
				url : "${pageContext.request.contextPath}/updateUser",
				type : "post",
				data : {
					userId : id,
					userPassword : '123456'
				},
				dataType : "json",
				success : function(data) {
					if (data.result === 1) {
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				},
				error : function() {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 5,
						time : 800
					});
				}
			});
		});
	}
</script>
</html>
